<template>
  <div style="margin-top: 50px">
    <el-form :model="state" ref="productSaleForm" label-width="120px" class="form-inner-container" size="small">
      <el-form-item label="赠送积分：">
        <el-input v-model="state.giftPoint"></el-input>
      </el-form-item>
      <el-form-item label="赠送成长值：">
        <el-input v-model="state.giftGrowth"></el-input>
      </el-form-item>
      <el-form-item label="积分购买限制：">
        <el-input v-model="state.usePointLimit"></el-input>
      </el-form-item>
      <el-form-item label="预告商品：">
        <el-switch v-model="state.previewStatus" :active-value="1" :inactive-value="0"> </el-switch>
      </el-form-item>
      <el-form-item label="商品上架：">
        <el-switch v-model="state.publishStatus" :active-value="1" :inactive-value="0"> </el-switch>
      </el-form-item>
      <el-form-item label="商品推荐：">
        <span style="margin-right: 10px">新品</span>
        <el-switch v-model="state.newStatus" :active-value="1" :inactive-value="0"> </el-switch>
        <span style="margin-left: 10px; margin-right: 10px">推荐</span>
        <el-switch v-model="state.recommandStatus" :active-value="1" :inactive-value="0"> </el-switch>
      </el-form-item>
      <el-form-item label="服务保证：">
        <el-checkbox-group v-model="state.selectServiceList">
          <el-checkbox :label="1">无忧退货</el-checkbox>
          <el-checkbox :label="2">快速退款</el-checkbox>
          <el-checkbox :label="3">免费包邮</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="详细页标题：">
        <el-input v-model="state.detailTitle"></el-input>
      </el-form-item>
      <el-form-item label="详细页描述：">
        <el-input v-model="state.detailDesc"></el-input>
      </el-form-item>
      <el-form-item label="商品关键字：">
        <el-input v-model="state.keywords"></el-input>
      </el-form-item>
      <el-form-item label="商品备注：">
        <el-input v-model="state.note" type="textarea" :autoSize="true"></el-input>
      </el-form-item>
      <el-form-item label="选择优惠方式：">
        <el-radio-group v-model="state.promotionType" size="small">
          <el-radio-button :label="0">无优惠</el-radio-button>
          <el-radio-button :label="1">特惠促销</el-radio-button>
          <el-radio-button :label="2">会员价格</el-radio-button>
          <el-radio-button :label="3">阶梯价格</el-radio-button>
          <el-radio-button :label="4">满减价格</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-show="state.promotionType === 1">
        <div class="littleMargin">
          开始时间：
          <el-config-provider :locale="locale">
            <el-date-picker v-model="state.promotionStartTime" type="datetime" :disabledDate="pickerOptions1" placeholder="选择开始时间"></el-date-picker>
          </el-config-provider>
        </div>
        <div class="littleMargin">
          结束时间：
          <el-config-provider :locale="locale">
            <el-date-picker v-model="state.promotionEndTime" type="datetime" :disabledDate="pickerOptions1" placeholder="选择结束时间"></el-date-picker>
          </el-config-provider>
        </div>
        <div class="littleMargin">
          促销价格：
          <el-input style="width: 220px" v-model="state.promotionPrice" placeholder="输入促销价格"></el-input>
        </div>
      </el-form-item>
      <el-form-item v-show="state.promotionType === 2">
        <div v-for="(item, index) in state.memberPriceList" :key="item.id" :class="{ littleMargin: index !== 0 }">
          {{ item.memberLevelName }}：
          <el-input v-model="item.memberPrice" style="width: 200px"></el-input>
        </div>
      </el-form-item>
      <el-form-item v-show="state.promotionType === 3">
        <el-table :data="state.productLadderList" style="width: 80%" border>
          <el-table-column label="数量" align="center" width="120">
            <template v-slot="scope">
              <el-input v-model="scope.row.count"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="折扣" align="center" width="120">
            <template v-slot="scope">
              <el-input v-model="scope.row.discount"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template v-slot="scope">
              <el-button size="default" @click="handleRemoveProductLadder(scope.$index, scope.row)">删除</el-button>
              <el-button size="default" @click="handleAddProductLadder(scope.$index, scope.row)">添加</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item v-show="state.promotionType === 4">
        <el-table :data="state.productFullReductionList" style="width: 80%" border>
          <el-table-column label="满" align="center" width="120">
            <template v-slot="scope">
              <el-input v-model="scope.row.fullPrice"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="立减" align="center" width="120">
            <template v-slot="scope">
              <el-input v-model="scope.row.reducePrice"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template v-slot="scope">
              <el-button size="default" @click="handleRemoveFullReduction(scope.$index, scope.row)">删除</el-button>
              <el-button size="default" @click="handleAddFullReduction(scope.$index, scope.row)">添加</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item style="text-align: center">
        <el-button size="default" @click="handlePrev">上一步，填写商品信息</el-button>
        <el-button type="primary" size="default" @click="handleNext">下一步，填写商品属性</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { fetchList as fetchMemberLevelList } from '@/plugins/memberLevel'
import { reactive, onBeforeMount } from 'vue'
import { ElMessage } from 'element-plus'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

const props = defineProps({
  value: Object,
  isEdit: {
    type: Boolean,
    default: false
  }
})

const locale = zhCn

const emits = defineEmits(['nextStep', 'prevStep'])

let state = reactive(props.value)

let pickerOptions1 = time => {
  return time.getTime() < Date.now()
}

onBeforeMount(() => {
  if (props.isEdit) {
    handleEditCreated()
  } else {
    fetchMemberLevelList({ defaultStatus: 0 }).then(response => {
      let memberPriceList = []
      if (response.data != null && response.data != undefined) {
        for (let i = 0; i < response.data.length; i++) {
          let item = response.data[i]
          memberPriceList.push({ memberLevelId: item.id, memberLevelName: item.name })
        }
        state.memberPriceList = memberPriceList
      }
    })
  }
})

function handleEditCreated() {
  if (state.serviceIds != null && state.serviceIds != undefined && state.serviceIds.length > 0) {
    state.selectServiceList = []
    let ids = state.serviceIds.split(',')
    for (let i = 0; i < ids.length; i++) {
      state.selectServiceList.push(Number(ids[i]))
    }
  }
}

function handleRemoveProductLadder(index) {
  let productLadderList = state.productLadderList
  if (productLadderList.length === 1) {
    productLadderList.pop()
    productLadderList.push({
      count: 0,
      discount: 0,
      price: 0
    })
  } else {
    productLadderList.splice(index, 1)
  }
}
function handleAddProductLadder() {
  let productLadderList = state.productLadderList
  if (productLadderList.length < 3) {
    productLadderList.push({
      count: 0,
      discount: 0,
      price: 0
    })
  } else {
    ElMessage({
      message: '最多只能添加三条',
      type: 'warning'
    })
  }
}
function handleRemoveFullReduction(index) {
  let fullReductionList = state.productFullReductionList
  if (fullReductionList.length === 1) {
    fullReductionList.pop()
    fullReductionList.push({
      fullPrice: 0,
      reducePrice: 0
    })
  } else {
    fullReductionList.splice(index, 1)
  }
}

function handleAddFullReduction() {
  let fullReductionList = state.productFullReductionList
  if (fullReductionList.length < 3) {
    fullReductionList.push({
      fullPrice: 0,
      reducePrice: 0
    })
  } else {
    ElMessage({
      message: '最多只能添加三条',
      type: 'warning'
    })
  }
}

function handlePrev() {
  emits('prevStep', state)
}

function handleNext() {
  emits('nextStep', state)
}
</script>

<style scoped>
.littleMargin {
  margin-top: 10px;
}
</style>
